//
//  PlistTest.m
//  MacTestProject
//
//  Created by LiuLichen on 2022/4/12.
//

#import "PlistTest.h"

@implementation PlistTest

- (id) init {
    self = [super init];

    [self testReadWriteDictionary];
    [self testReadWriteArray];

    return self;
}

- (void) testReadWriteDictionary {
    NSString* path = @"/Users/lichenliu/Documents/MacStudy/ios_study/MacTestProject/MacTestProject/File/dictionary.plist";
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithContentsOfFile: path];
    NSLog(@"%@", dict);
    NSObject* obj = @{
        @"age": @"18",
        @"job": @"student"
    };
    [dict setObject: obj forKey: @"chris"];
    [dict writeToFile: path atomically: YES];
    dict = [NSMutableDictionary dictionaryWithContentsOfFile: path];
    NSLog(@"%@", dict);
}

- (void) testReadWriteArray {
    NSString* path = @"/Users/lichenliu/Documents/MacStudy/ios_study/MacTestProject/MacTestProject/File/array.plist";
    NSMutableArray* arr = [NSMutableArray arrayWithContentsOfFile: path];
    NSLog(@"%@", arr);
    if (![arr containsObject: @"three"]) {
        [arr addObject: @"three"];
    }
    [arr writeToFile: path atomically: YES];
    arr = [NSMutableArray arrayWithContentsOfFile: path];
    NSLog(@"%@", arr);
}

@end
